<?php
class PhanloaisinhviensController extends AppController {

	var $name = 'Phanloaisinhviens';
	var $uses = array('Phanloaisinhvien','Xeploai','Sinhvien','Chidoansv','Namhoc','Hockie','Thanhtichsv','Thanhtichkhoasv','Z1phanloaisinhvien','Z1dvutsinhvien','Xeploaicd','Loaicdsinhvien','Loaicdcb');
	var $helpers=array('FlashChart');
    	    
    /**
     * function _loaiSinhVien()
     * Phan loai doan vien sinh vien dua vao table Xeploais with input (diemrenluyen,diemtbhk)
     * Ket qua tra ve la id cua table Xeploais
     * id	=> 	xeploai
     * 1	=>	Xuat sac
     * 2	=>	Kha
     * 3	=>	Trung binh
     * 4	=>	Yeu
     * Rang buoc 0<=diemrenluyen<=100, 0<=diemtbhk<=100 da thuc hien o buoc nhap nen o day khong co dieu kien rang buoc giup cho code "clean and clear"
     */
    function _loaiSinhVien($diemrenluyen,$diemtbhk){
        $kq = $this->Xeploai->find('all');
        if($diemrenluyen>=$kq[0]['Xeploai']['diemrenluyen'] && $diemtbhk>=$kq[0]['Xeploai']['diemtbhk'])
        	$xeploai = $kq[0]['Xeploai']['id'];//Xuat sac
        	
        	else if($diemrenluyen>=$kq[1]['Xeploai']['diemrenluyen'] && $diemtbhk>=$kq[1]['Xeploai']['diemtbhk'])
        		$xeploai = $kq[1]['Xeploai']['id'];//Kha 
        		
        		else if($diemrenluyen>=$kq[2]['Xeploai']['diemrenluyen'] && $diemtbhk>=$kq[2]['Xeploai']['diemtbhk'])
        			$xeploai = $kq[2]['Xeploai']['id'];//Trung binh
        			
        			else if($diemrenluyen>=$kq[3]['Xeploai']['diemrenluyen'] && $diemtbhk>=$kq[3]['Xeploai']['diemtbhk'])
        				$xeploai = $kq[3]['Xeploai']['id'];//Yeu
        					
        					else $xeploai = "Something wrong";//Khac cac truong hop tren se thong bao loi
        return $xeploai;
    }
    
	/**
     * function phanloai()
     * Phan loai toan bo doan vien 
     * Vi chua co user nen khong the xac dinh duoc chi doan nao de phan loai tung chi doan
     * */    
	function phanloai(){
        $namhocht_id=$this->Session->read('namhocht_id');
        //Lay danh sach sinh vien can phan loai || Can phai them dieu kien de xet theo chi doan.
		$dsphanloai = $this->Z1phanloaisinhvien->find('all',array('conditions'=>array('namhoc_id'=>$namhocht_id,'chidoansv_id'=>$this->Session->read('chidoanht_id'))));
        //pr($dsphanloai);
		foreach($dsphanloai as $phanloai){
		  if($phanloai['Z1phanloaisinhvien']['duyet']==0){
			$xeploai=$this->_loaiSinhVien($phanloai['Z1phanloaisinhvien']['diemrenluyen'],$phanloai['Z1phanloaisinhvien']['diemtbhk']);
		 	$sinhvien_id=$phanloai['Z1phanloaisinhvien']['sinhvien_id'];
		 	$this->Phanloaisinhvien->updateAll(
    			array('Phanloaisinhvien.xeploai_id' => $xeploai),
    			array('Phanloaisinhvien.sinhvien_id ' => $sinhvien_id)
			);
           }
		}
        $this->redirect(array('action' => 'index'));
		//pr($this->_loaiSinhVien(100,3.29));//edit value _loaiSinhVien(diemrenluyen,diemtbhk);
	}  
     
    /**
     *     	Liet ke danh sanh cua doan vien trong cung 1 hoc ki	
     *     	Table Hockys de lay ngay bat dau va ngay ket thuc
     *     	Table phong
     *     	Tim Hockys dua tren dk hocki_id nen se tra ve 1 hocki duy nhat
     *     	Khi dua tham so hocki_id vao se tim duoc thoi gian bat dau va ket thuc, dua vao do lay ngay ket thuc cua phong trao (thanhtichsv) de liet ke danh sach phong trao thoa dk
     *     	$hk_id = '1';//Hocki_id la tham so duoc truyen tu ngoai vao ham
     *     	$mssv = '1081636';
     */
	function thanhtichSV($mssv,$hk_id){
       	//$hk_id = '1';//Hocki_id la tham so duoc truyen tu ngoai vao ham
       	//$mssv = '1081636';
		//Ngay bat dau Ngay ket thuc hoc ki	
		$hocki = $this->Hocky->find('all', array('conditions' => array('Hocky.id' => $hk_id)));
		$x = $hocki[0]['Hocky']['ngaybatdau'];
		$y = $hocki[0]['Hocky']['ngayketthuc'];
		
		//Thanhtichsv
		$thanhtich = $this->Thanhtichsv->find('all', array('conditions' => array(
																				'Sinhvien.id' => $mssv,
																				array('Phongtraosv.ngayketthuc BETWEEN ? AND ?' => array($x,$y))
													)));
		return count($thanhtich);
        //pr($thanhtich);
	}    
	
	/**
		Nhap vao MSSV va HOCKI_ID tra ve so thanh tich trong hoc ky cua mot sv khi biet duoc mssv va hocky.
        mssv: nhap vao
        hoc ky: lay id tu hoc ky hien tai.
	*/
	function _thanhtichKhoasv($mssv,$hk_id){
	   
        $hocki = $this->Hocky->find('all', array('conditions' => array('Hocky.id' => $hk_id)));
        $x = $hocki[0]['Hocky']['ngaybatdau'];
        $y = $hocki[0]['Hocky']['ngayketthuc'];
        $thanhtichkhoa = $this->Thanhtichkhoasv->find('all', array('conditions' => array(
        																'Sinhvien.id' => $mssv,
        																array('Phongtraokhoa.ngayketthuc BETWEEN ? AND ?' => array($x,$y))
                                                      )));
        return count($thanhtichkhoa);
	}  
    
	/**
     * function index()
     * Loc ra danh sach sinhvien co cung ma lop voi bi thu, va trong mot học ky xac dinh
     * */    
	function index() {
		$this->Z1phanloaisinhvien->recursive = -1;
        $xeploais = $this->Xeploai->find('list',array('fields'=>'tenloai'));
        $this->set('xeploais', $xeploais);
        $namhocht_id=$this->Session->read('namhocht_id');
        $chidoan_id=$this->Session->read('chidoanht_id');
        $phanloaisinhviens=$this->Z1phanloaisinhvien->find('all',array('conditions'=>array('namhoc_id'=>$namhocht_id,'chidoansv_id'=>$chidoan_id)));
        $tb=0;$y=0;$xs=0;$k=0;
        foreach($phanloaisinhviens as $phanloaisinhvien){
            if($phanloaisinhvien['Z1phanloaisinhvien']['xeploai_id']==1)$xs++;
             else if($phanloaisinhvien['Z1phanloaisinhvien']['xeploai_id']==2) $k++;
             else if($phanloaisinhvien['Z1phanloaisinhvien']['xeploai_id']==3) $tb++;
             else if($phanloaisinhvien['Z1phanloaisinhvien']['xeploai_id']==4)$y++;
        }
        $tong=count($phanloaisinhviens);
        if($tong>0){
            $tlxs=($xs/$tong)*100;
            $tlk=($k/$tong)*100;
            $tltb=($tb/$tong)*100;
            $tly=($y/$tong)*100;    
        }else {
            $tlxs=0;
            $tlk=0;
            $tltb=0;
            $tly=0;
        }
        $loaicdsinhvien=$this->Xeploaicd->find('list',array('fields'=>'loaicd'));
        $loaicd=$this->__phanloaicd($tlxs,$tlk,$tltb,$tly);
        $check=$this->Loaicdsinhvien->find('first',array('conditions'=>array('namhoc_id'=>$namhocht_id,'chidoansv_id'=>$chidoan_id),'recursive'=>-1));
        //pr($check);
        if($check==null){
            $this->Loaicdsinhvien->saveAll(array('xeploaicd_id'=>$loaicd,'namhoc_id'=>$namhocht_id,'chidoansv_id'=>$chidoan_id));    
        }else $loaicd=$check['Loaicdsinhvien']['xeploaicd_id'];
        $this->set(compact('phanloaisinhviens','tlxs','tlk','tltb','tly','tong','loaicd','loaicdsinhvien'));
	}
    
    /**
     * function capnhat()
     * Update ket qua cap nhat sau khi bi thu check lai lan cuoi
     * */
    function capnhat(){
        //pr($this->data);exit;
        for($j=1;$j<count($this->data['Phanloaisinhvien'])/2;$j++){
            //echo $this->data['Phanloaisinhvien']['xeploai_id'.$j];
            $this->Phanloaisinhvien->updateAll(
    			array('Phanloaisinhvien.xeploai_id' => $this->data['Phanloaisinhvien']['xeploai_id'.$j]),
    			array('Phanloaisinhvien.sinhvien_id ' => $this->data['Phanloaisinhvien']['sinhvien_id'.$j], 'Phanloaisinhvien.duyet'=>0)
			);
        }
        $this->redirect(array('action' => 'index'));
    }
    
    /**
     * function __phanloaicd()
     * Get cac gia tri % doan vien xs, tb, y, kha de xep loai chi doan do
     * luu ket qua xep loai vao bang Loaicdsinhviens
     * */   
     function __phanloaicd($tlxs,$tlk,$tltb,$tly){
        $xeploaicd=$this->Xeploaicd->find('all');        
        //pr($xeploaicd);
        $data=$this->data['Phanloaisinhvien'];
        //pr($data);
        $xeploaivm=$xeploaicd[0]['Xeploaicd'];
        $xeploaik=$xeploaicd[1]['Xeploaicd'];
        $xeploaitb=$xeploaicd[2]['Xeploaicd'];
        $xeploaiy=$xeploaicd[3]['Xeploaicd'];
        $loaicd=0;
        if(($tlxs>=$xeploaiy['xs1']&&$tlxs<=$xeploaiy['xs2'])&&($tlk>=$xeploaiy['k1']&&$tlk<=$xeploaiy['k2'])&&($tltb>=$xeploaiy['tb1']&&$tltb<=$xeploaiy['tb2'])&&($tly>=$xeploaiy['y1']&&$tly<=$xeploaiy['y2'])){
            $loaicd=4;//'Chi đoàn yếu kém';        
        }else if(($tlxs>=$xeploaitb['xs1']&&$tlxs<=$xeploaitb['xs2'])&&($tlk>=$xeploaitb['k1']&&$tlk<=$xeploaitb['k2'])&&($tltb>=$xeploaitb['tb1']&&$tltb<=$xeploaitb['tb2'])&&($tly>=$xeploaitb['y1']&&$tly<=$xeploaitb['y2'])){
            $loaicd=3;//'Chi đoàn trung bình';
        }else if(($tlxs>=$xeploaik['xs1']&&$tlxs<=$xeploaik['xs2'])&&($tlk>=$xeploaik['k1']&&$tlk<=$xeploaik['k2'])&&($tltb>=$xeploaik['tb1']&&$tltb<=$xeploaik['tb2'])&&($tly>=$xeploaik['y1']&&$tly<=$xeploaik['y2'])){ 
            $loaicd=2;//'Chi đoàn khá';
        }else if(($tlxs>=$xeploaivm['xs1']&&$tlxs<=$xeploaivm['xs2'])&&($tlk>=$xeploaivm['k1']&&$tlk<=$xeploaivm['k2'])&&($tltb>=$xeploaivm['tb1']&&$tltb<=$xeploaivm['tb2'])&&($tly>=$xeploaivm['y1']&&$tly<=$xeploaivm['y2'])){
            $loaicd=1;//'Chi đoàn vững mạnh';
        }else $loaicd=1;//'Chi đoàn vững mạnh';
        return $loaicd;
     }
     
     /**
     * function danhsachcd()
     * Liet ke danh sach cac chi doan de duyet
     * */   
    function danhsachcd(){
        $khoa_id=$this->Session->read('khoa_id');
        $chidoansvs=$this->Chidoansv->find('all',array('conditions'=>array('khoahoc_id <>'=>null,'khoa_id'=>$khoa_id)));
        $this->set(compact('chidoansvs'));
    }
    /**
     * function duyetcd($cdid=null)
     * The hien form pldv cua chi doan dang duyet
     * $cdid la id cua chi doan dang duyet
     * */ 
    function duyetcd($cdid=null){
        $chidoan=$this->Chidoansv->find('first',array('conditions'=>array('Chidoansv.id'=>$cdid),'recursive'=>-1));
        $this->set('chidoan',$chidoan);
        $this->Z1phanloaisinhvien->recursive = -1;
        $xeploais = $this->Xeploai->find('all',array('fields'=>array('id','tenloai'),'recursive'=>-1));
        $this->set('xeploais', $xeploais);
        $namhocht_id=$this->Session->read('namhocht_id');
        //$chidoan_id=$this->Session->read('chidoanht_id');
        $phanloaisinhviens=$this->Z1phanloaisinhvien->find('all',array('conditions'=>array('namhoc_id'=>$namhocht_id,'chidoansv_id'=>$cdid)));
        $tb=0;$y=0;$xs=0;$k=0;
        foreach($phanloaisinhviens as $phanloaisinhvien){
            if($phanloaisinhvien['Z1phanloaisinhvien']['xeploai_id']==1)$xs++;
             else if($phanloaisinhvien['Z1phanloaisinhvien']['xeploai_id']==2) $k++;
             else if($phanloaisinhvien['Z1phanloaisinhvien']['xeploai_id']==3) $tb++;
             else $y++;
        }
        $tong=count($phanloaisinhviens);
        if($tong>0){
            $tlxs=($xs/$tong)*100;
            $tlk=($k/$tong)*100;
            $tltb=($tb/$tong)*100;
            $tly=($y/$tong)*100;    
        }else {
            $tlxs=0;
            $tlk=0;
            $tltb=0;
            $tly=0;
        }
        $loaicdsinhvien=$this->Xeploaicd->find('all',array('fields'=>array('id','loaicd'),'recursive'=>-1));
        $check=$this->Loaicdsinhvien->find('first',array('conditions'=>array('namhoc_id'=>$namhocht_id,'chidoansv_id'=>$cdid),'recursive'=>-1));        
        $loaicd=$check['Loaicdsinhvien']['xeploaicd_id'];
        $namhocs=$this->Namhoc->find('list',array('fields'=>array('namhoc'),'recursive'=>-1));
        $this->set(compact('phanloaisinhviens','tlxs','tlk','tltb','tly','tong','loaicd','loaicdsinhvien','namhocs'));  
        
        //duyet doan vien uu tu
       $dvutsvs=$this->Z1dvutsinhvien->find('all', array('conditions'=> array('namhoc_id'=>$namhocht_id,'chidoansv_id'=>$cdid)));
       $chidoan=$this->Chidoansv->find('first',array('conditions'=>array('Chidoansv.id'=>$cdid),'recursive'=>-1));
       $this->set(compact('dvutsvs','chidoan'));      
    }
    function thongke($namhoc=null,$cdid=null){
        $this->layout='search';
        $namhoc=$this->Namhoc->find('first',array('conditions'=>array('namhoc'=>$namhoc),'recursive'=>-1));//pr($hk);exit;
        $phanloaisinhviens=$this->Z1phanloaisinhvien->find('all',array('conditions'=>array('namhoc_id'=>$namhoc['Namhoc']['id'],'chidoansv_id'=>$cdid)));
        $this->set(compact('phanloaisinhviens','namhoc'));
        $chidoan=$this->Chidoansv->find('first',array('conditions'=>array('Chidoansv.id'=>$cdid),'recursive'=>-1));
        $this->set('chidoan',$chidoan);
        $this->Z1phanloaisinhvien->recursive = -1;
        $xeploais = $this->Xeploai->find('all',array('fields'=>array('id','tenloai'),'recursive'=>-1));
        $this->set('xeploais', $xeploais);
        //$chidoan_id=$this->Session->read('chidoanht_id');
        $phanloaisinhviens=$this->Z1phanloaisinhvien->find('all',array('conditions'=>array('namhoc_id'=>$namhoc['Namhoc']['id'],'chidoansv_id'=>$cdid)));
        $tb=0;$y=0;$xs=0;$k=0;
        foreach($phanloaisinhviens as $phanloaisinhvien){
            if($phanloaisinhvien['Z1phanloaisinhvien']['xeploai_id']==1)$xs++;
             else if($phanloaisinhvien['Z1phanloaisinhvien']['xeploai_id']==2) $k++;
             else if($phanloaisinhvien['Z1phanloaisinhvien']['xeploai_id']==3) $tb++;
             else $y++;
        }
        $tong=count($phanloaisinhviens);
        if($tong>0){
            $tlxs=($xs/$tong)*100;
            $tlk=($k/$tong)*100;
            $tltb=($tb/$tong)*100;
            $tly=($y/$tong)*100;    
        }else {
            $tlxs=0;
            $tlk=0;
            $tltb=0;
            $tly=0;
        }
        $loaicdsinhvien=$this->Xeploaicd->find('all',array('fields'=>array('id','loaicd'),'recursive'=>-1));
        $check=$this->Loaicdsinhvien->find('first',array('conditions'=>array('namhoc_id'=>$namhoc['Namhoc']['id'],'chidoansv_id'=>$cdid),'recursive'=>-1));        
        $loaicd=$check['Loaicdsinhvien']['xeploaicd_id'];
        $hockys=$this->Hocky->find('all',array('fields'=>array('tenhocky'),'recursive'=>-1));
        $dvutsvs=$this->Z1dvutsinhvien->find('all', array('conditions'=> array('namhoc_id'=>$namhoc['Namhoc']['id'],'chidoansv_id'=>$cdid)));
        $this->set(compact('tlxs','tlk','tltb','tly','tong','loaicd','Loaicdsinhvien','hockys','dvutsvs'));
    }
    /**
     * function duyet()
     * Duyet 1 hoac nhiu sinh vien cung luc
     * Co 2 chuc nang: duyet, mo khoa
     * */ 
    function duyet(){
        $namhocht_id=$this->Session->read('namhocht_id');
        $cd=$this->data['Phanloaisinhvien']['chidoan_id'];
        //pr($this->data);exit;
        $chuaduyetids = array();              
		foreach($this->data['Phanloaisinhvien']['chuaduyet'] as $id => $value){
			if($value == 1){
	  		  $chuaduyetids[] = $id;
			}
		}
        $duyetids = array();
        foreach($this->data['Phanloaisinhvien']['duyet'] as $id => $value){
			if($value == 1){
	  		  $duyetids[] = $id;
			}
		}
        //pr($chuaduyetids);exit;
		if (count($duyetids) == 0 && count($chuaduyetids) == 0  ) {
			$this->Session->setFlash(__('Khong co sinh vien nao duoc chon', true));
			$this->redirect(array('action'=>'duyetcd',$cd));
		}else {
            $this->Loaicdsinhvien->updateAll(array('Loaicdsinhvien.duyet'=>1),array('Loaicdsinhvien.chidoansv_id'=>$cd,'Loaicdsinhvien.namhoc_id'=>$namhocht_id));
    		$this->Phanloaisinhvien->updateAll(array('Phanloaisinhvien.duyet'=>1),array('Phanloaisinhvien.sinhvien_id'=>$duyetids));
            $this->Phanloaisinhvien->updateAll(array('Phanloaisinhvien.duyet'=>0),array('Phanloaisinhvien.sinhvien_id'=>$chuaduyetids));
			$this->Session->setFlash(__('Đã lưu !', true));
			$this->redirect(array('action'=>'duyetcd',$cd));	
		}
    }
    function reviewkqpldv(){
        $cdid=$this->Session->read('chidoanht_id');
        $this->Z1phanloaisinhvien->recursive = -1;
        $xeploais = $this->Xeploai->find('all',array('fields'=>array('id','tenloai'),'recursive'=>-1));
        $this->set('xeploais', $xeploais);
        $namhocht_id=$this->Session->read('namhocht_id');
        $phanloaisinhviens=$this->Z1phanloaisinhvien->find('all',array('conditions'=>array('namhoc_id'=>$namhocht_id,'chidoansv_id'=>$cdid)));
        $tb=0;$y=0;$xs=0;$k=0;
        foreach($phanloaisinhviens as $phanloaisinhvien){
            if($phanloaisinhvien['Z1phanloaisinhvien']['xeploai_id']==1)$xs++;
             else if($phanloaisinhvien['Z1phanloaisinhvien']['xeploai_id']==2) $k++;
             else if($phanloaisinhvien['Z1phanloaisinhvien']['xeploai_id']==3) $tb++;
             else $y++;
        }
        $tong=count($phanloaisinhviens);
        if($tong>0){
            $tlxs=($xs/$tong)*100;
            $tlk=($k/$tong)*100;
            $tltb=($tb/$tong)*100;
            $tly=($y/$tong)*100;    
        }else {
            $tlxs=0;
            $tlk=0;
            $tltb=0;
            $tly=0;
        }
        $dvutsvs=$this->Z1dvutsinhvien->find('all', array('conditions'=> array('namhoc_id'=>$namhocht_id,'chidoansv_id'=>$cdid)));        
        $loaicdsinhvien=$this->Xeploaicd->find('all',array('fields'=>array('id','loaicd'),'recursive'=>-1));
        $check=$this->Loaicdsinhvien->find('first',array('conditions'=>array('namhoc_id'=>$namhocht_id,'chidoansv_id'=>$cdid),'recursive'=>-1));
        $loaicd=$check['Loaicdsinhvien']['xeploaicd_id'];
        $this->set(compact('phanloaisinhviens','tlxs','tlk','tltb','tly','tong','loaicd','loaicdsinhvien','dvutsvs'));        
    }
    function ketquapldv(){
        $this->layout='excel';
        $cdid=$this->Session->read('chidoanht_id');
        $this->Z1phanloaisinhvien->recursive = -1;
        $xeploais = $this->Xeploai->find('all',array('fields'=>array('id','tenloai'),'recursive'=>-1));
        $this->set('xeploais', $xeploais);
        $namhocht_id=$this->Session->read('namhocht_id');
        $phanloaisinhviens=$this->Z1phanloaisinhvien->find('all',array('conditions'=>array('namhoc_id'=>$namhocht_id,'chidoansv_id'=>$cdid)));
        $tb=0;$y=0;$xs=0;$k=0;
        foreach($phanloaisinhviens as $phanloaisinhvien){
            if($phanloaisinhvien['Z1phanloaisinhvien']['xeploai_id']==1)$xs++;
             else if($phanloaisinhvien['Z1phanloaisinhvien']['xeploai_id']==2) $k++;
             else if($phanloaisinhvien['Z1phanloaisinhvien']['xeploai_id']==3) $tb++;
             else $y++;
        }
        $tong=count($phanloaisinhviens);
        if($tong>0){
            $tlxs=($xs/$tong)*100;
            $tlk=($k/$tong)*100;
            $tltb=($tb/$tong)*100;
            $tly=($y/$tong)*100;    
        }else {
            $tlxs=0;
            $tlk=0;
            $tltb=0;
            $tly=0;
        }
        $dvutsvs=$this->Z1dvutsinhvien->find('all', array('conditions'=> array('namhoc_id'=>$namhocht_id,'chidoansv_id'=>$cdid)));        
        $loaicdsinhvien=$this->Xeploaicd->find('all',array('fields'=>array('id','loaicd'),'recursive'=>-1));
        $check=$this->Loaicdsinhvien->find('first',array('conditions'=>array('namhoc_id'=>$namhocht_id,'chidoansv_id'=>$cdid),'recursive'=>-1));
        $loaicd=$check['Loaicdsinhvien']['xeploaicd_id'];
        $this->set(compact('Phanloaisinhviens','tlxs','tlk','tltb','tly','tong','loaicd','Loaicdsinhvien','dvutsvs'));        
    }
    function reviewall(){
        $namhocht_id=$this->Session->read('namhocht_id');
        $khoa_id=$this->Session->read('khoa_id');
        $namhoc_id=$this->Session->read('namhocht_id');
        $phanloaisinhviens=$this->Z1phanloaisinhvien->find('all',array('conditions'=>array('namhoc_id'=>$namhocht_id)));
        $this->loadModel('Z1plcb');
        $phanloaicbs=$this->Z1plcb->find('all',array('conditions'=>array('namhoc_id'=>$namhoc_id)));
        $chidoans=$this->Chidoansv->find('all',array('conditions'=>array('khoa_id'=>$khoa_id)));
        $chidoancbs=$this->Chidoansv->find('all',array('conditions'=>array('khoa_id'=>$khoa_id,'khoahoc_id'=>null)));
        $xeploais = $this->Xeploai->find('all',array('fields'=>array('id','tenloai'),'recursive'=>-1));
        $loaicdsinhviens=$this->Loaicdsinhvien->find('all',array('conditions'=>array('namhoc_id'=>$namhocht_id)));
        $loaicdcbs=$this->Loaicdcb->find('all',array('conditions'=>array('namhoc_id'=>$namhoc_id)));
        //pr($loaicdsinhviens);
        //pr($phanloaisinhviens);
        $this->set(compact('phanloaicbs','phanloaisinhviens','chidoans','chidoancbs','xeploais','loaicdsinhviens','loaicdcbs'));
    }
    function ketquapldvall(){
        $this->layout = 'excel';
        $namhocht_id=$this->Session->read('namhocht_id');
        $khoa_id=$this->Session->read('khoa_id');
        $namhoc_id=$this->Session->read('namhocht_id');
        $phanloaisinhviens=$this->Z1phanloaisinhvien->find('all',array('conditions'=>array('namhoc_id'=>$namhocht_id)));
        $this->loadModel('Z1plcb');
        $phanloaicbs=$this->Z1plcb->find('all',array('conditions'=>array('namhoc_id'=>$namhoc_id)));
        $chidoans=$this->Chidoansv->find('all',array('conditions'=>array('khoa_id'=>$khoa_id)));
        $chidoancbs=$this->Chidoansv->find('all',array('conditions'=>array('khoa_id'=>$khoa_id,'khoahoc_id'=>null)));
        $xeploais = $this->Xeploai->find('all',array('fields'=>array('id','tenloai'),'recursive'=>-1));
        $loaicdsinhviens=$this->Loaicdsinhvien->find('all',array('conditions'=>array('namhoc_id'=>$namhocht_id)));
        $loaicdcbs=$this->Loaicdcb->find('all',array('conditions'=>array('namhoc_id'=>$namhoc_id)));
        //pr($loaicdsinhviens);
        //pr($phanloaisinhviens);
        $this->set(compact('phanloaicbs','phanloaisinhviens','chidoans','chidoancbs','xeploais','loaicdsinhviens','loaicdcbs'));
    }
    function tilepldv($namhoc_id=0,$hocky_id = 0)
    {
    	$khoaht_id=$this->Session->read('khoa_id');
        $chidoanht=$this->Session->read('chidoanht_id');
        $info=$this->Chidoansv->find('first',array('conditions'=>array('Chidoansv.id'=>$chidoanht),'recursive'=>-1));
        if(!empty($this->data))
        {
        	$namhoc_id =$this->data['Phanloaisinhvien']['namhoc'] ;
        	$hocky_id =$this->data['Phanloaisinhvien']['hocky'] ;
        	$this->redirect(array('action'=>'tilepldv',$namhoc_id,$hocky_id));
        }
         if($namhoc_id==0)
			$namhoc_id = $this->Session->read('namhocht_id');
    	if($hocky_id==0)
        {
        	$namhocht_id=$this->Session->read('namhocht_id');
        	$hocky_id = $hk['Hocky']['id'];
        }
        if($khoaht_id==$info['Chidoansv']['khoa_id']){
            $this->Z1phanloaisinhvien->recursive = -1;
            $phanloaisinhviens=$this->Z1phanloaisinhvien->find('all',array('conditions'=>array('hocky_id'=>$hocky_id)));
            $tb=0;$y=0;$xs=0;$k=0;
            foreach($phanloaisinhviens as $phanloaisinhvien){
                if($phanloaisinhvien['Z1phanloaisinhvien']['xeploai_id']==1)$xs++;
                 else if($phanloaisinhvien['Z1phanloaisinhvien']['xeploai_id']==2) $k++;
                 else if($phanloaisinhvien['Z1phanloaisinhvien']['xeploai_id']==3) $tb++;
                 else $y++;
            }
            $tong=count($phanloaisinhviens);
            if($tong>0){
                $tlxs=round(($xs/$tong),2)*100;
                $tlk=round(($k/$tong),2)*100;
                $tltb=round(($tb/$tong),2)*100;
                $tly=round(($y/$tong),2)*100;    
            }else {
                $tlxs=0;
                $tlk=0;
                $tltb=0;
                $tly=0;
            }
            $this->Namhoc->displayField='namhoc';
    		$namhocs = $this->Namhoc->find('list');
    		$hockys = $this->Hocky->find('list',array('conditions'=>array('namhoc_id'=>$namhoc_id),'fields'=>array('tenhocky')));
            $tyles = array(array('loai'=>'Xuất sắc','tyle'=>$tlxs),array('loai'=>'Khá','tyle'=>$tlk),array('loai'=>'Trung bình','tyle'=>$tltb),array('loai'=>'Yếu','tyle'=>$tly));
            $this->set(compact('tyles','tong','namhocs','hockys','namhoc_id','hocky_id'));   
        }else{
            $this->Session->setFlash(__('Invalid! Khong thuoc khoa cua ban', true));
        }
             
    }
    
    function tiledrl($namhoc_id=0,$hocky_id = 0)
    {
        $khoaht_id=$this->Session->read('khoa_id');
        $chidoanht=$this->Session->read('chidoanht_id');
        $info=$this->Chidoansv->find('first',array('conditions'=>array('Chidoansv.id'=>$chidoanht),'recursive'=>-1));
        if(!empty($this->data))
        {
        	$namhoc_id =$this->data['Phanloaisinhvien']['namhoc'] ;
        	$hocky_id =$this->data['Phanloaisinhvien']['hocky'] ;
        	$this->redirect(array('action'=>'tiledrl',$namhoc_id,$hocky_id));
        }
        if($khoaht_id==$info['Chidoansv']['khoa_id']){
            $this->Z1phanloaisinhvien->recursive = -1;
            if($namhoc_id==0)
    			$namhoc_id = $this->Session->read('namhocht_id');
            if($hocky_id==0)
            {
            	$namhocht_id=$this->Session->read('namhocht_id');
            	$hocky_id = $hk['Hocky']['id'];
            }
            $phanloaisinhviens=$this->Z1phanloaisinhvien->find('all',array('conditions'=>array('hocky_id'=>$hocky_id)));
            
            $tb=0;$y=0;$xs=0;$k=0;$g=0;
            foreach($phanloaisinhviens as $phanloaisinhvien){
                if($phanloaisinhvien['Z1phanloaisinhvien']['diemrenluyen']<=60)$y++;
                 else if($phanloaisinhvien['Z1phanloaisinhvien']['diemrenluyen']<=70) $k++;
                 else if($phanloaisinhvien['Z1phanloaisinhvien']['diemrenluyen']<=80) $g++;
                 else $xs++;
            }
            $tong=count($phanloaisinhviens);
            if($tong>0){
                $tlxs=(round($xs/$tong,2)*100);
                $tlk=(round($k/$tong,2)*100);
                $tltb=(round($tb/$tong,2)*100);
                $tly=(round($y/$tong,2)*100);
                $tlg=(round($g/$tong,2)*100);      
            }else
            {
                $tlxs=0;
                $tlk=0;
                $tltb=0;
                $tly=0;
                $tlg=0;
            }
            $this->Namhoc->displayField='namhoc';
    		$namhocs = $this->Namhoc->find('list');
    		$hockys = $this->Hocky->find('list',array('conditions'=>array('namhoc_id'=>$namhoc_id),'fields'=>array('tenhocky')));
            $tyles = array(array('loai'=>'Xuất sắc','tyle'=>$tlxs),array('loai'=>'Giỏi','tyle'=>$tlg),array('loai'=>'Khá','tyle'=>$tlk),array('loai'=>'Trung bình','tyle'=>$tltb),array('loai'=>'Yếu','tyle'=>$tly));
            $this->set(compact('tyles','tong','namhocs','hockys','namhoc_id','hocky_id'));        
      }else{
            $this->Session->setFlash(__('Invalid! Khong thuoc khoa cua ban', true));
        }
    }
    
    function tiletbhk($namhoc_id=0,$hocky_id = 0)
    {
    	$khoaht_id=$this->Session->read('khoa_id');
        $chidoanht=$this->Session->read('chidoanht_id');
        $info=$this->Chidoansv->find('first',array('conditions'=>array('Chidoansv.id'=>$chidoanht),'recursive'=>-1));
        if(!empty($this->data))
        {
        	$namhoc_id =$this->data['Phanloaisinhvien']['namhoc'] ;
        	$hocky_id =$this->data['Phanloaisinhvien']['hocky'] ;
        	$this->redirect(array('action'=>'tiletbhk',$namhoc_id,$hocky_id));
        }
        if($namhoc_id==0)
			$namhoc_id = $this->Session->read('namhocht_id');
        if($hocky_id==0)
        {
        	$namhocht_id=$this->Session->read('namhocht_id');
        	$hocky_id = $hk['Hocky']['id'];
        }
        if($khoaht_id==$info['Chidoansv']['khoa_id']){
            $this->Z1phanloaisinhvien->recursive = -1;
            $phanloaisinhviens=$this->Z1phanloaisinhvien->find('all',array('conditions'=>array('hocky_id'=>$hocky_id)));
            $tb=0;$y=0;$xs=0;$k=0;$g=0;
            foreach($phanloaisinhviens as $phanloaisinhvien){
                if($phanloaisinhvien['Z1phanloaisinhvien']['diemtbhk']<=2.0)$y++;
                 else if($phanloaisinhvien['Z1phanloaisinhvien']['diemtbhk']<=2.5) $k++;
                 else if($phanloaisinhvien['Z1phanloaisinhvien']['diemtbhk']<=3.2) $g++;
                 else $xs++;
            }
            $tong=count($phanloaisinhviens);
            if($tong>0){
                $tlxs=round(($xs/$tong),2)*100;
                $tlk=round(($k/$tong),2)*100;
                $tltb=round(($tb/$tong),2)*100;
                $tly=round(($y/$tong),2)*100;
                $tlg=round(($g/$tong),2)*100;      
            }else {
                $tlxs=0;
                $tlk=0;
                $tltb=0;
                $tly=0;
                $tlg=0;
            }
            $this->Namhoc->displayField='namhoc';
    		$namhocs = $this->Namhoc->find('list');
    		$hockys = $this->Hocky->find('list',array('conditions'=>array('namhoc_id'=>$namhoc_id),'fields'=>array('tenhocky')));
            $tyles = array(array('loai'=>'Xuất sắc','tyle'=>$tlxs),array('loai'=>'Giỏi','tyle'=>$tlg),array('loai'=>'Khá','tyle'=>$tlk),array('loai'=>'Trung bình','tyle'=>$tltb),array('loai'=>'Yếu','tyle'=>$tly));
            $this->set(compact('tyles','tong','namhocs','hockys','namhoc_id','hocky_id'));
      }else{
            $this->Session->setFlash(__('Invalid! Khong thuoc khoa cua ban', true));
        }
    }
    
     
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Phanloaisinhvien', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('Phanloaisinhvien', $this->Phanloaisinhvien->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Phanloaisinhvien->create();
			if ($this->Phanloaisinhvien->save($this->data)) {
				$this->Session->setFlash(__('The Phanloaisinhvien has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The Phanloaisinhvien could not be saved. Please, try again.', true));
			}
		}
		$xeploais = $this->Phanloaisinhvien->Xeploai->find('list');
		$sinhviens = $this->Phanloaisinhvien->Sinhvien->find('list');
		$hockys = $this->Phanloaisinhvien->Hocky->find('list');
		$this->set(compact('xeploais', 'sinhviens', 'hockys'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Phanloaisinhvien', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Phanloaisinhvien->save($this->data)) {
				$this->Session->setFlash(__('The Phanloaisinhvien has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The Phanloaisinhvien could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Phanloaisinhvien->read(null, $id);
		}
		$xeploais = $this->Phanloaisinhvien->Xeploai->find('list');
		$sinhviens = $this->Phanloaisinhvien->Sinhvien->find('list');
		$hockys = $this->Phanloaisinhvien->Hocky->find('list');
		$this->set(compact('xeploais', 'sinhviens', 'hockys'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Phanloaisinhvien', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Phanloaisinhvien->delete($id)) {
			$this->Session->setFlash(__('Phanloaisinhvien deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Phanloaisinhvien was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}

	function updatehocky()
	{
		$this->layout='ajax';
		$namhoc_id = $this->data['Phanloaisinhvien']['namhoc'];
		$hockys = $this->Hocky->find('list',array('conditions'=>array('namhoc_id'=>$namhoc_id),'fields'=>array('tenhocky')));
		$this->set(compact('hockys'));
	}
}